package com.dc.datacollect.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;

public class ResourcesFileUtils {
    private static String toPath(String relDir, String fullFilename) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        String resourcesPath = classLoader.getResource("").getPath().substring(1);
        String filePath = (resourcesPath + "/" + relDir + "/" + fullFilename).replaceAll("/+", "/");
        return filePath;
    }
    public static File file(String relDir, String fullFilename) {
        String path = toPath(relDir, fullFilename);
        File file = new File(path);
        File parentDir = file.getParentFile();
        if (! parentDir.exists()) {
            parentDir.mkdirs();
        }
        return file;
    }
    public static boolean removeFile(String relDir, String fullFilename) {
        String path = toPath(relDir, fullFilename);
        File file = new File(path);
        return file.delete();
    }

}
